import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;


public class test01 {
    @Test
    public void selectAll() throws IOException {
        String resource="MyBatis.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory =new SqlSessionFactoryBuilder().build(inputStream);
        SqlSession sqlSession = sqlSessionFactory.openSession();
        List<User> users=sqlSession.selectList("test.selectAll");
        System.out.println(users);
        sqlSession.close();
    }
    @Test
    public void selectOne() throws IOException {
        String resource="MyBatis.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory =new SqlSessionFactoryBuilder().build(inputStream);
        SqlSession sqlSession = sqlSessionFactory.openSession();
        List<User> users=sqlSession.selectList("test.selectOne",1);
        System.out.println(users);
        sqlSession.close();
    }
    @Test
    public void delete() throws IOException {
        String resource="MyBatis.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory =new SqlSessionFactoryBuilder().build(inputStream);
        SqlSession sqlSession = sqlSessionFactory.openSession();
        sqlSession.selectOne("test.delete",3);
        sqlSession.commit();
        selectAll();
        sqlSession.close();
    }
    @Test
    public void add() throws IOException {
        String resource="MyBatis.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory =new SqlSessionFactoryBuilder().build(inputStream);
        SqlSession sqlSession = sqlSessionFactory.openSession();
        User user = new User();
        user.setUsername("无奈又");
        user.setGender("男");
        user.setAddr("福建");
        user.setPassword("1123");
        sqlSession.selectOne("test.add",user);
        sqlSession.commit();
        selectAll();
        sqlSession.close();
    }
    @Test
    public void update() throws IOException {
        String resource="MyBatis.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory =new SqlSessionFactoryBuilder().build(inputStream);
        SqlSession sqlSession = sqlSessionFactory.openSession();
        User user = new User(1,"张四2","111","女","江东");
        sqlSession.selectList("test.update",user);
        sqlSession.commit();
        selectAll();
        sqlSession.close();
    }
}
